package com.middleware.mq;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class Consumer1 {

    public static void main(String[] args) throws Exception{

        //1. 创建连接工厂
        ConnectionFactory connectionFactory = new
                ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_BROKER_URL);
        //2. 创建并启动连接
        Connection connection = connectionFactory.createConnection();
        connection.start();
        //3. 创建Session
        Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
        //4. 创建目的地对象
        Destination destination = session.createQueue("weixin-Queue");
        //5. 创建消费者
        MessageConsumer consumer = session.createConsumer(destination);
        //6. 获取消息
        consumer.setMessageListener(new MessageListener() {
        @Override
        public void onMessage(Message message) {
        TextMessage textMessage = (TextMessage) message;
        try {
            System.out.println(textMessage.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        } }
        });
        System.in.read();
        //7. 释放资源
        consumer.close();
        session.close();
        connection.close();
    }

}
